package com.ht.dao;

import com.ht.bean.Emp;
import com.ht.bean.Holiday;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


public interface HolidayDao {

    /*
     *请假
     * @param
     * @return
     **/

    @Insert("insert into Holiday values(null,#{empId},#{holidayDay},NOW(),#{endTime},#{title},#{status},#{remark},#{processInstanceId},#{hdays},#{hours})")
    @SelectKey(before=false,keyColumn = "holidayId",keyProperty = "holidayId",statement = "select last_insert_id()",resultType = Integer.class)
    boolean addApplay(Holiday holiday);

    //查询我的申请单
    @Select("select * from Holiday where empId=#{empId}")
    public List<Holiday> selHoliday(String empId);

    @Select({"<script>",
            "select * from holiday h, emp e ",
            "<where>",
            " h.empId = e.empId",
            "<if test=\"holiday.holidayId!=null and holiday.holidayId!=''\">",
            "and h.holidayId = #{holiday.holidayId}" ,
            "</if>" ,
            "<if test=\"emp.empName!=null and emp.empName!=''\">",
            "and empName like concat('%',#{emp.empName},'%')" ,
            "</if>" ,
            "</where>" ,
            "limit #{startIndex},#{pageSize}" ,
            "</script>"})
    List<Map> selectHoliday(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize, @Param("emp") Emp emp, @Param("holiday") Holiday holiday);

    /*
     *计算出员工表数据总数
     * 条件搜索
     * @param
     * @return
     **/
    @Select({"<script>" ,
            "select count(*) from holiday h, emp e ",
            "<where>" ,
            " h.empId = e.empId",
            "<if test=\"holiday.holidayId!=null and holiday.holidayId!=''\">",
            "and h.holidayId = #{holiday.holidayId}" ,
            "</if>" ,
            "<if test=\"emp.empName!=null and emp.empName!=''\">",
            "and empName like concat('%',#{emp.empName},'%')" ,
            "</if>" ,
            "</where>" ,
            "</script>"})
    int countEmp(@Param("emp") Emp emp,@Param("holiday") Holiday holiday);

    @Select("select * from Holiday where holidayId=#{holidayId}")
    public Holiday findObjById(Integer holidayId);

    //更改表单状态
    @Update("update Holiday set status=#{status} where holidayId=#{holidayId}")
    public boolean updHoliday(Holiday holiday);

    @Select("SELECT d.chairman \n" +
            "FROM emp AS e INNER JOIN dep AS d\n" +
            "ON e.depId = d.depId where e.empId = #{empId} limit 1")
    String selectempName(String empId);

    @Select("select empId from emp where empid =(select chairman from dep where parentId = 0)")
    String selectempName2();

    @Select("select * from emp")
    List<Emp> empList();

    //删除员工请假信息
    @Delete("delete  from holiday where holidayId = #{holidayId}")
    boolean deleteHolidayId(Integer holidayId);

    @Select("select h.*,e.empName,d.depName,e.phone from emp e,Holiday h,dep d " +
            "where e.empId = h.empId and e.depId = d.depid and h.holidayId=#{holidayId}")
    List<Map> LookHolidayContent(Integer holidayId);

}
